<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是kernel——open函数系统调用过程中，dentry结构的创建以及如何通过inode查找和关联的过程。从d_alloc_parallel()开始，经d_alloc()、__d_alloc()函数，最终在ext4_lookup()中找到inode，并在d_splice_alias()和__d_add()函数中完成dentry与inode的绑定，确保后续lookup快速定位。若笔记中有错">
<meta property="og:type" content="article">
<meta property="og:title" content="LV05-03-Kernel-05-03-03-open函数解析2">
<meta property="og:url" content="https://sumumm.github.io/post/d22d4c9b.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是kernel——open函数系统调用过程中，dentry结构的创建以及如何通过inode查找和关联的过程。从d_alloc_parallel()开始，经d_alloc()、__d_alloc()函数，最终在ext4_lookup()中找到inode，并在d_splice_alias()和__d_add()函数中完成dentry与inode的绑定，确保后续lookup快速定位。若笔记中有错">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241214223720583.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241214231751874.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241225095155651.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241225104517989.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241225113903443.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241225114110536.png">
<meta property="article:published_time" content="2024-12-17T15:25:19.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.045Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV05-系统镜像">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241214223720583.png">


<link rel="canonical" href="https://sumumm.github.io/post/d22d4c9b.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":"","permalink":"https://sumumm.github.io/post/d22d4c9b.html","path":"post/d22d4c9b.html","title":"LV05-03-Kernel-05-03-03-open函数解析2"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV05-03-Kernel-05-03-03-open函数解析2 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81open%E5%87%BD%E6%95%B0%E8%B0%83%E7%94%A8%E5%9B%9E%E9%A1%BE"><span class="nav-text">一、open函数调用回顾</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81walk-component-%E5%88%86%E6%9E%90"><span class="nav-text">二、walk_component()分析</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-lookup-slow"><span class="nav-text">1. lookup_slow()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-d-alloc-parallel"><span class="nav-text">1.1 d_alloc_parallel()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-1-d-alloc"><span class="nav-text">1.1.1 d_alloc()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-2-d-alloc"><span class="nav-text">1.1.2 __d_alloc()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-3-%E6%80%BB%E7%BB%93"><span class="nav-text">1.1.3 总结</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-ext4-lookup"><span class="nav-text">1.2 ext4_lookup()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-1-d-splice-alias"><span class="nav-text">1.2.1 d_splice_alias()</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-2-%E6%80%BB%E7%BB%93"><span class="nav-text">1.2.2 总结</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81-lookup-open"><span class="nav-text">三、 lookup_open()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E6%80%BB%E7%BB%93"><span class="nav-text">四、总结</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/d22d4c9b.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV05-03-Kernel-05-03-03-open函数解析2 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV05-03-Kernel-05-03-03-open函数解析2
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-12-17 23:25:19" itemprop="dateCreated datePublished" datetime="2024-12-17T23:25:19+08:00">2024-12-17</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/" itemprop="url" rel="index"><span itemprop="name">02IMX6ULL平台</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/" itemprop="url" rel="index"><span itemprop="name">LV05-系统镜像</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>4.1k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>15 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是kernel——open函数系统调用过程中，dentry结构的创建以及如何通过inode查找和关联的过程。从d_alloc_parallel()开始，经d_alloc()、__d_alloc()函数，最终在ext4_lookup()中找到inode，并在d_splice_alias()和__d_add()函数中完成dentry与inode的绑定，确保后续lookup快速定位。若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" rowspan="5">PC端开发环境</td>        <td align="center" width=150px>Windows</td>        <td align="left">Windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu20.04.2的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 17 Pro</td>        <td align="left">17.6.0 build-24238078</td>      </tr>    <tr>        <td align="center">终端软件</td>        <td align="left">MobaXterm(Professional Edition v23.0 Build 5042 (license))</td>    </tr>    <tr>        <td align="center">Win32DiskImager</td>        <td align="left">Win32DiskImager v1.0</td>      </tr>    <tr>        <td align="center" rowspan="3">Linux开发板环境</td>        <td align="center">Linux开发板</td>        <td align="left">正点原子 i.MX6ULL Linux 阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，使用的uboot版本为U-Boot 2019.04</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.19.71(NXP官方提供)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="5">官方网站</td>        <td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td>        <td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/" target="_blank">https://www.nxp.com.cn/ </a></td>        <td align="left">NXP官方网站</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxpic.org.cn/" target="_blank">https://www.nxpic.org.cn/</a></td><td align="left">NXP 官方社区</td>    </tr>    <tr>        <td align="left"><a href="https://u-boot.readthedocs.io/en/latest/" target="_blank">https://u-boot.readthedocs.io/en/latest/</a></td><td align="left">u-boot官网</td>    </tr>    <tr>        <td align="left"><a href="https://www.kernel.org/" target="_blank">https://www.kernel.org/</a></td><td align="left">linux内核官网</td>    </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="3">NXP</td>        <td align="left"><a href="https://github.com/nxp-imx" target="_blank">https://github.com/nxp-imx</a></td>        <td align="left">NXP imx开发资源GitHub组织，里边会有u-boot和linux内核的仓库</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/linux-imx/releases/tag/v4.19.71" target="_blank">nxp-imx/linux-imx/releases/tag/v4.19.71</a></td>        <td align="left">NXP linux内核仓库tags中的v4.19.71</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/uboot-imx/releases/tag/rel_imx_4.19.35_1.1.0" target="_blank">nxp-imx/uboot-imx/releases/tag/rel_imx_4.19.35_1.1.0</a></td>        <td align="left">NXP u-boot仓库tags中的rel_imx_4.19.35_1.1.0</td>    </tr>    <tr>        <td align="center" rowspan="2">I.MX6ULL</td>        <td align="left"><a href="https://www.nxp.com.cn/docs/en/data-sheet/IMX6ULLIEC.pdf" target="_blank">i.MX 6ULL Applications Processors for Industrial Products</a></td>        <td align="left">I.MX6ULL 芯片手册（datasheet，可以在线查看）</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/webapp/Download?colCode=IMX6ULLRM&lang_cd=zh" target="_blank">i.MX 6ULL Applications ProcessorReference Manual</a></td>        <td align="left">I.MX6ULL 参考手册（下载后才能查看，需要登录NXP官网）</td>    </tr>    <tr>        <td align="center" rowspan="2">Source Code</td>        <td align="left"><a href="https://elixir.bootlin.com/linux/latest/source" target="_blank">https://elixir.bootlin.com/linux/latest/source</a></td>        <td align="left">linux kernel源码</td>    </tr>    <tr>        <td align="left"><a href="https://elixir.bootlin.com/u-boot/latest/source" target="_blank">https://elixir.bootlin.com/u-boot/latest/source</a></td>        <td align="left">uboot源码</td>    </tr></table>
              </div>
            </details>

<h1 id="一、open函数调用回顾"><a href="#一、open函数调用回顾" class="headerlink" title="一、open函数调用回顾"></a><font size=3>一、open函数调用回顾</font></h1><p>这里还是先以打开前面的demo为例：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/stat.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span>* argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> fd = open(<span class="string">&quot;/home/sumu/7Linux/test.txt&quot;</span>, O_CREAT | O_RDWR);</span><br><span class="line">    close(fd);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>回顾一下open函数的调用过程：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241214223720583.png" alt="image-20241214223720583" />

<p>在《01嵌入式开发&#x2F;02IMX6ULL平台&#x2F;LV05-系统镜像&#x2F;LV05-03-Kernel-05-03-02-open函数解析1.md》里面我们以打开 “&#x2F;home&#x2F;sumu&#x2F;7Linux&#x2F;test.txt” 为例分析了系统调用”open”的内核实现。其中我们假设了路径名中所有部分的dentry结构体均已存在在系统缓存中。即通过 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1554">lookup_fast()</a> 函数成功找到目标文件的dentry。那要是这个函数调用失败了，没有找到目标文件的dentry怎么办？这一节就来分析一下吧。</p>
<h1 id="二、walk-component-分析"><a href="#二、walk-component-分析" class="headerlink" title="二、walk_component()分析"></a><font size=3>二、walk_component()分析</font></h1><p>我们直接来到 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1789">walk_component()</a> 函数中：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241214231751874.png" alt="image-20241214231751874" />

<p>在 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1789">walk_component()</a> 函数通过调用 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1554">lookup_fast()</a> 函数失败时，进入 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1681">lookup_slow()</a> 函数。在 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1681">lookup_slow()</a> 函数为目标文件创建一个新的dentry，并加入到系统缓存中。现来分析其具体过程，例如假设文件夹“sumu”尚不存在在系统缓存中（虽然这种情况几乎不存在）。</p>
<blockquote>
<p>说明：在这里我们仍然不考虑函数调用失败和进程同步等问题）</p>
</blockquote>
<h2 id="1-lookup-slow"><a href="#1-lookup-slow" class="headerlink" title="1. lookup_slow()"></a><font size=3>1. lookup_slow()</font></h2><p> <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1681">lookup_slow()</a> 函数定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="keyword">struct</span> dentry *<span class="title function_">lookup_slow</span><span class="params">(<span class="type">const</span> <span class="keyword">struct</span> qstr *name,</span></span><br><span class="line"><span class="params">				  <span class="keyword">struct</span> dentry *dir,</span></span><br><span class="line"><span class="params">				  <span class="type">unsigned</span> <span class="type">int</span> flags)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">inode</span> *<span class="title">inode</span> =</span> dir-&gt;d_inode;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">dentry</span> *<span class="title">res</span>;</span></span><br><span class="line">	inode_lock_shared(inode);</span><br><span class="line">	res = __lookup_slow(name, dir, flags);</span><br><span class="line">	inode_unlock_shared(inode);</span><br><span class="line">	<span class="keyword">return</span> res;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以看到 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1681">lookup_slow()</a> 函数内部调用了 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1642">__lookup_slow()</a> 函数：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* Fast lookup failed, do it the slow way */</span></span><br><span class="line"><span class="type">static</span> <span class="keyword">struct</span> <span class="title class_">dentry</span> *__lookup_slow(<span class="type">const</span> <span class="keyword">struct</span> qstr *name,</span><br><span class="line">				    <span class="keyword">struct</span> dentry *dir,</span><br><span class="line">				    <span class="type">unsigned</span> <span class="type">int</span> flags)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">dentry</span> *dentry, *old;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">inode</span> *inode = dir-&gt;d_inode;</span><br><span class="line">	<span class="built_in">DECLARE_WAIT_QUEUE_HEAD_ONSTACK</span>(wq);</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* Don&#x27;t go there if it&#x27;s already dead */</span></span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">unlikely</span>(<span class="built_in">IS_DEADDIR</span>(inode))) <span class="comment">// if 判断为 0</span></span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line">again:</span><br><span class="line">	dentry = <span class="built_in">d_alloc_parallel</span>(dir, name, &amp;wq);</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">IS_ERR</span>(dentry)) <span class="comment">// if 判断为 0</span></span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">unlikely</span>(!<span class="built_in">d_in_lookup</span>(dentry))) &#123; <span class="comment">// 不考虑并发性问题， if 判断为 0</span></span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		old = inode-&gt;i_op-&gt;<span class="built_in">lookup</span>(inode, dentry, flags);<span class="comment">// 一般情况下，返回NULL</span></span><br><span class="line">		<span class="built_in">d_lookup_done</span>(dentry);</span><br><span class="line">		<span class="keyword">if</span> (<span class="built_in">unlikely</span>(old)) &#123; <span class="comment">// if 判断为 0</span></span><br><span class="line">			<span class="comment">//......</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> dentry;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-1-d-alloc-parallel"><a href="#1-1-d-alloc-parallel" class="headerlink" title="1.1 d_alloc_parallel()"></a><font size=3>1.1 d_alloc_parallel()</font></h3><p>先来分析其中的 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n2434">d_alloc_parallel()</a> 函数。在我们分析的这种情况下，该函数定义等价与如下，（该函数涉及很多并发性问题的考虑，参考<a target="_blank" rel="noopener" href="https://lwn.net/Articles/692546/">《Parallel pathname lookups and the importance of testing》</a> ）：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">dentry</span> *<span class="built_in">d_alloc_parallel</span>(<span class="keyword">struct</span> dentry *parent,</span><br><span class="line">				<span class="type">const</span> <span class="keyword">struct</span> qstr *name,</span><br><span class="line">				<span class="type">wait_queue_head_t</span> *wq)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">unsigned</span> <span class="type">int</span> hash = name-&gt;hash;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">hlist_bl_head</span> *b = <span class="built_in">in_lookup_hash</span>(parent, hash);</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">hlist_bl_node</span> *node;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">dentry</span> *<span class="keyword">new</span> = <span class="built_in">d_alloc</span>(parent, name);</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">dentry</span> *dentry;</span><br><span class="line">	<span class="type">unsigned</span> seq, r_seq, d_seq;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">unlikely</span>(!<span class="keyword">new</span>)) <span class="comment">// if 判断为 0</span></span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line">        </span><br><span class="line">retry:</span><br><span class="line">	<span class="built_in">rcu_read_lock</span>();</span><br><span class="line">	seq = <span class="built_in">smp_load_acquire</span>(&amp;parent-&gt;d_inode-&gt;i_dir_seq);</span><br><span class="line">	r_seq = <span class="built_in">read_seqbegin</span>(&amp;rename_lock);</span><br><span class="line">	dentry = __d_lookup_rcu(parent, name, &amp;d_seq);</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">unlikely</span>(dentry)) &#123; <span class="comment">// if 判断为 0（dentry 刚刚被创建和初始化，但尚未被加入哈希表中 (不考虑并发性问题））</span></span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">unlikely</span>(<span class="built_in">read_seqretry</span>(&amp;rename_lock, r_seq))) &#123;<span class="comment">// 暂不考虑并发性问题，假设此时无需 retry， if 判断为 0</span></span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">unlikely</span>(seq &amp; <span class="number">1</span>)) &#123;</span><br><span class="line">		<span class="built_in">rcu_read_unlock</span>();</span><br><span class="line">		<span class="keyword">goto</span> retry;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">hlist_bl_lock</span>(b);</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">unlikely</span>(<span class="built_in">READ_ONCE</span>(parent-&gt;d_inode-&gt;i_dir_seq) != seq)) &#123; <span class="comment">// 暂不考虑，假设 if 判断为 0</span></span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	 * No changes for the parent since the beginning of d_lookup().</span></span><br><span class="line"><span class="comment">	 * Since all removals from the chain happen with hlist_bl_lock(),</span></span><br><span class="line"><span class="comment">	 * any potential in-lookup matches are going to stay here until</span></span><br><span class="line"><span class="comment">	 * we unlock the chain.  All fields are stable in everything</span></span><br><span class="line"><span class="comment">	 * we encounter.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">    <span class="comment">// 遍历 in_lookup_hashtable，在不考虑并发性的问题情况下，新创建的这个 dentry 在 in_lookup_hashtable 中也找不到</span></span><br><span class="line">	<span class="built_in">hlist_bl_for_each_entry</span>(dentry, node, b, d_u.d_in_lookup_hash) &#123;</span><br><span class="line">		<span class="keyword">if</span> (dentry-&gt;d_name.hash != hash) <span class="comment">//if 判断始终为 1， 直到整个遍历结束</span></span><br><span class="line">			<span class="keyword">continue</span>;</span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">rcu_read_unlock</span>();</span><br><span class="line">	<span class="comment">/* we can&#x27;t take -&gt;d_lock here; it&#x27;s OK, though. */</span></span><br><span class="line">	<span class="keyword">new</span>-&gt;d_flags |= DCACHE_PAR_LOOKUP;</span><br><span class="line">	<span class="keyword">new</span>-&gt;d_wait = wq;</span><br><span class="line">    <span class="comment">// 将新创建的 dentry 加入到 in_lookup_hashtable 中，以便并发访问的其他程序能够找到，不会重复创建相同 dentry</span></span><br><span class="line">	<span class="built_in">hlist_bl_add_head_rcu</span>(&amp;<span class="keyword">new</span>-&gt;d_u.d_in_lookup_hash, b);</span><br><span class="line">	<span class="built_in">hlist_bl_unlock</span>(b);</span><br><span class="line">	<span class="keyword">return</span> <span class="keyword">new</span>; <span class="comment">// 函数返回</span></span><br><span class="line">mismatch:</span><br><span class="line">	<span class="comment">//......</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">EXPORT_SYMBOL</span>(d_alloc_parallel);</span><br></pre></td></tr></table></figure>

<h4 id="1-1-1-d-alloc"><a href="#1-1-1-d-alloc" class="headerlink" title="1.1.1 d_alloc()"></a><font size=3>1.1.1 d_alloc()</font></h4><p>先看 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1684">d_alloc()</a> 函数。在目前分析的情况下该函数等价如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * d_alloc	-	allocate a dcache entry</span></span><br><span class="line"><span class="comment"> * @parent: parent of entry to allocate</span></span><br><span class="line"><span class="comment"> * @name: qstr of the name</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * Allocates a dentry. It returns %NULL if there is insufficient memory</span></span><br><span class="line"><span class="comment"> * available. On a success the dentry is returned. The name passed in is</span></span><br><span class="line"><span class="comment"> * copied and the copy passed in may be reused after this call.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">dentry</span> *<span class="built_in">d_alloc</span>(<span class="keyword">struct</span> dentry * parent, <span class="type">const</span> <span class="keyword">struct</span> qstr *name)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">dentry</span> *dentry = __d_alloc(parent-&gt;d_sb, name);</span><br><span class="line">	<span class="keyword">if</span> (!dentry)</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">	<span class="built_in">spin_lock</span>(&amp;parent-&gt;d_lock);</span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	 * don&#x27;t need child lock because it is not subject</span></span><br><span class="line"><span class="comment">	 * to concurrency here</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	__dget_dlock(parent);</span><br><span class="line">	dentry-&gt;d_parent = parent;</span><br><span class="line">	<span class="built_in">list_add</span>(&amp;dentry-&gt;d_child, &amp;parent-&gt;d_subdirs);<span class="comment">//将新创建的 dentry-&gt;d_child 加入 父目录的 dentry-&gt;d_subdirs 链后面，此处不做详细分析</span></span><br><span class="line">	<span class="built_in">spin_unlock</span>(&amp;parent-&gt;d_lock);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> dentry;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">EXPORT_SYMBOL</span>(d_alloc);</span><br></pre></td></tr></table></figure>

<h4 id="1-1-2-d-alloc"><a href="#1-1-2-d-alloc" class="headerlink" title="1.1.2 __d_alloc()"></a><font size=3>1.1.2 __d_alloc()</font></h4><p>看一下 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1684">d_alloc()</a> 中的<a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1594">__d_alloc()</a> 函数，在这里分析如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">dentry</span> *__d_alloc(<span class="keyword">struct</span> super_block *sb, <span class="type">const</span> <span class="keyword">struct</span> qstr *name)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">external_name</span> *ext = <span class="literal">NULL</span>;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">dentry</span> *dentry;</span><br><span class="line">	<span class="type">char</span> *dname;</span><br><span class="line">	<span class="type">int</span> err;</span><br><span class="line"></span><br><span class="line">	dentry = <span class="built_in">kmem_cache_alloc</span>(dentry_cache, GFP_KERNEL);</span><br><span class="line">	<span class="keyword">if</span> (!dentry)</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	 * We guarantee that the inline name is always NUL-terminated.</span></span><br><span class="line"><span class="comment">	 * This way the memcpy() done by the name switching in rename</span></span><br><span class="line"><span class="comment">	 * will still always have a NUL at the end, even if we might</span></span><br><span class="line"><span class="comment">	 * be overwriting an internal NUL character</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	dentry-&gt;d_iname[DNAME_INLINE_LEN<span class="number">-1</span>] = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">unlikely</span>(!name)) &#123;<span class="comment">//这里if 判断结果为0</span></span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line">	&#125; <span class="keyword">else</span> <span class="keyword">if</span> (name-&gt;len &gt; DNAME_INLINE_LEN<span class="number">-1</span>) &#123;<span class="comment">//DNAME_INLINE_LEN=32,name ~ sumu/7Linux/test.txt, if 判断为0</span></span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line">	&#125; <span class="keyword">else</span>  &#123;</span><br><span class="line">		dname = dentry-&gt;d_iname;</span><br><span class="line">	&#125;	</span><br><span class="line"></span><br><span class="line">	dentry-&gt;d_name.len = name-&gt;len;</span><br><span class="line">	dentry-&gt;d_name.hash = name-&gt;hash;</span><br><span class="line">	<span class="built_in">memcpy</span>(dname, name-&gt;name, name-&gt;len);</span><br><span class="line">	dname[name-&gt;len] = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* Make sure we always see the terminating NUL character */</span></span><br><span class="line">	<span class="built_in">smp_store_release</span>(&amp;dentry-&gt;d_name.name, dname); <span class="comment">/* ^^^ */</span></span><br><span class="line">	<span class="comment">// 到这里，由于 name-&gt;len 小于 dentry-&gt;d_iname 数组的大小，因此 dentry-&gt;d_iname 和 dentry-&gt;d_name 指向的是同一个名字(sumu/7Linux/test.txt)</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">//下面是根据参数 struct qstr *name 和 struct super_block *sb 提供的信息初始化刚刚创建的 dentry</span></span><br><span class="line">	dentry-&gt;d_lockref.count = <span class="number">1</span>;</span><br><span class="line">	dentry-&gt;d_flags = <span class="number">0</span>;</span><br><span class="line">	<span class="built_in">spin_lock_init</span>(&amp;dentry-&gt;d_lock);</span><br><span class="line">	<span class="built_in">seqcount_init</span>(&amp;dentry-&gt;d_seq);</span><br><span class="line">	dentry-&gt;d_inode = <span class="literal">NULL</span>; <span class="comment">// 注意，d_inode 没有赋值，整个dentry只能算是个空壳...</span></span><br><span class="line">	dentry-&gt;d_parent = dentry;</span><br><span class="line">	dentry-&gt;d_sb = sb;</span><br><span class="line">	dentry-&gt;d_op = <span class="literal">NULL</span>;</span><br><span class="line">	dentry-&gt;d_fsdata = <span class="literal">NULL</span>;</span><br><span class="line">	<span class="built_in">INIT_HLIST_BL_NODE</span>(&amp;dentry-&gt;d_hash);</span><br><span class="line">	<span class="built_in">INIT_LIST_HEAD</span>(&amp;dentry-&gt;d_lru);</span><br><span class="line">	<span class="built_in">INIT_LIST_HEAD</span>(&amp;dentry-&gt;d_subdirs);</span><br><span class="line">	<span class="built_in">INIT_HLIST_NODE</span>(&amp;dentry-&gt;d_u.d_alias);</span><br><span class="line">	<span class="built_in">INIT_LIST_HEAD</span>(&amp;dentry-&gt;d_child);</span><br><span class="line">	<span class="built_in">d_set_d_op</span>(dentry, dentry-&gt;d_sb-&gt;s_d_op); <span class="comment">// 在本例中 dentry-&gt;d_op = sb-&gt;s_d_op （== NULL）</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (dentry-&gt;d_op &amp;&amp; dentry-&gt;d_op-&gt;d_init) &#123; <span class="comment">// dentry-&gt;d_op == 0</span></span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">unlikely</span>(ext)) &#123;</span><br><span class="line">		<span class="type">pg_data_t</span> *pgdat = <span class="built_in">page_pgdat</span>(<span class="built_in">virt_to_page</span>(ext));</span><br><span class="line">		<span class="built_in">mod_node_page_state</span>(pgdat, NR_INDIRECTLY_RECLAIMABLE_BYTES,</span><br><span class="line">				    <span class="built_in">ksize</span>(ext));</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">this_cpu_inc</span>(nr_dentry);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> dentry;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可见，在 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1594">__d_alloc()</a> 函数中，创建新的 struct dentry，并根据 父目录的dentry&rarr;d_sb 和本目录的 name 进行相关初始化。该函数返回之后，我们回到 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1684">d_alloc()</a> 函数。在 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1684">d_alloc()</a> 函数调用完毕 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1594">__d_alloc()</a> 函数之后，再对新创建的 dentry 进行简单设置之后便返回了，详见上面代码部分。</p>
<h4 id="1-1-3-总结"><a href="#1-1-3-总结" class="headerlink" title="1.1.3 总结"></a><font size=3>1.1.3 总结</font></h4><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241225095155651.png" alt="image-20241225095155651"  />

<p><a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1684">d_alloc()</a> 函数结束之后返回到 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n2434">d_alloc_parallel()</a> 函数。<a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n2434">d_alloc_parallel()</a> 函数执行完 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1684">d_alloc()</a> 函数之后，进行简单操作（不考虑并发性问题的考虑）之后，返回新创建的 dentry。<a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n2434">d_alloc_parallel()</a> 函数返回之后，便回到<a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1642">__lookup_slow()</a> 函数。</p>
<h3 id="1-2-ext4-lookup"><a href="#1-2-ext4-lookup" class="headerlink" title="1.2 ext4_lookup()"></a><font size=3>1.2 ext4_lookup()</font></h3><p>需要注意的是，在整个 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n2434">d_alloc_parallel()</a> 函数中，只是创建了一个新的dentry，但还没找到相应的inode，整个dentry目前只是一具空壳。下面将进入 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1671">inode&rarr;i_op&rarr;lookup()</a> 函数，在这个函数中，将找到相应的inode，并与dentry联系起来。这是一个函数指针，要分析该函数需知道该指针指向的函数。其实该指针指向的是<a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/ext4/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1560">ext4_lookup()</a> 函数。为什么？其实这个函数根据不同文件系统指向不同的函数，但是大概都叫 xxx_lookup()，我们可以在<a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4">fs - kernel&#x2F;git&#x2F;stable&#x2F;linux.git - Linux kernel stable tree</a>目录中搜索一下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">grep -nRw &quot;lookup&quot; ./fs/</span><br></pre></td></tr></table></figure>

<p>就会找到，若是ext4文件系统的话，就会指向这个<a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/ext4/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1560">ext4_lookup()</a> 函数，该函数定义如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="keyword">struct</span> <span class="title class_">dentry</span> *<span class="built_in">ext4_lookup</span>(<span class="keyword">struct</span> inode *dir, <span class="keyword">struct</span> dentry *dentry, <span class="type">unsigned</span> <span class="type">int</span> flags)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">inode</span> *inode;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">ext4_dir_entry_2</span> *de;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">buffer_head</span> *bh;</span><br><span class="line">	<span class="type">int</span> err;</span><br><span class="line"></span><br><span class="line">	err = <span class="built_in">fscrypt_prepare_lookup</span>(dir, dentry, flags);</span><br><span class="line">	<span class="keyword">if</span> (err) <span class="comment">// if 判断为 0</span></span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (dentry-&gt;d_name.len &gt; EXT4_NAME_LEN) <span class="comment">// if 判断为 0</span></span><br><span class="line">		<span class="comment">//......</span></span><br><span class="line">	<span class="comment">/********* 根据父目录和目标文件的文件名，找到目标文件的inode *********/</span></span><br><span class="line">	bh = <span class="built_in">ext4_find_entry</span>(dir, &amp;dentry-&gt;d_name, &amp;de, <span class="literal">NULL</span>);</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">IS_ERR</span>(bh))</span><br><span class="line">		<span class="keyword">return</span> (<span class="keyword">struct</span> dentry *) bh;</span><br><span class="line">	inode = <span class="literal">NULL</span>;</span><br><span class="line">	<span class="keyword">if</span> (bh) &#123;</span><br><span class="line">		__u32 ino = <span class="built_in">le32_to_cpu</span>(de-&gt;inode);</span><br><span class="line">		<span class="built_in">brelse</span>(bh);</span><br><span class="line">		<span class="keyword">if</span> (!<span class="built_in">ext4_valid_inum</span>(dir-&gt;i_sb, ino)) &#123;</span><br><span class="line">			<span class="built_in">EXT4_ERROR_INODE</span>(dir, <span class="string">&quot;bad inode number: %u&quot;</span>, ino);</span><br><span class="line">			<span class="keyword">return</span> <span class="built_in">ERR_PTR</span>(-EFSCORRUPTED);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (<span class="built_in">unlikely</span>(ino == dir-&gt;i_ino)) &#123;</span><br><span class="line">			<span class="built_in">EXT4_ERROR_INODE</span>(dir, <span class="string">&quot;&#x27;%pd&#x27; linked to parent dir&quot;</span>,</span><br><span class="line">					 dentry);</span><br><span class="line">			<span class="keyword">return</span> <span class="built_in">ERR_PTR</span>(-EFSCORRUPTED);</span><br><span class="line">		&#125;</span><br><span class="line">		inode = <span class="built_in">ext4_iget</span>(dir-&gt;i_sb, ino, EXT4_IGET_NORMAL);</span><br><span class="line">		<span class="keyword">if</span> (inode == <span class="built_in">ERR_PTR</span>(-ESTALE)) &#123;</span><br><span class="line">			<span class="built_in">EXT4_ERROR_INODE</span>(dir,</span><br><span class="line">					 <span class="string">&quot;deleted inode referenced: %u&quot;</span>,</span><br><span class="line">					 ino);</span><br><span class="line">			<span class="keyword">return</span> <span class="built_in">ERR_PTR</span>(-EFSCORRUPTED);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span> (!<span class="built_in">IS_ERR</span>(inode) &amp;&amp; <span class="built_in">ext4_encrypted_inode</span>(dir) &amp;&amp;</span><br><span class="line">		    (<span class="built_in">S_ISDIR</span>(inode-&gt;i_mode) || <span class="built_in">S_ISLNK</span>(inode-&gt;i_mode)) &amp;&amp;</span><br><span class="line">		    !<span class="built_in">fscrypt_has_permitted_context</span>(dir, inode)) &#123;</span><br><span class="line">			<span class="built_in">ext4_warning</span>(inode-&gt;i_sb,</span><br><span class="line">				     <span class="string">&quot;Inconsistent encryption contexts: %lu/%lu&quot;</span>,</span><br><span class="line">				     dir-&gt;i_ino, inode-&gt;i_ino);</span><br><span class="line">			<span class="built_in">iput</span>(inode);</span><br><span class="line">			<span class="keyword">return</span> <span class="built_in">ERR_PTR</span>(-EPERM);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">    <span class="comment">/*******************************************************************/</span></span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">d_splice_alias</span>(inode, dentry);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可见这个函数中大部分代码是用来找到目标文件的inode。因为在前面的代码中，只是创建了一个dentry的“空壳”，还没有与任何一个inode相关联，这个dentry的d_inode 被初始化为 dentry&rarr;d_inode &#x3D; NULL。在<a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/ext4/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1560">ext4_lookup()</a> 这个函数中找到对应的inode。这也符合这个函数的名字：lookup,就是要找到inode。这里寻找inode的过程依赖ext4文件系统的配置和技术细节，这里将不进行深入分析了。</p>
<h4 id="1-2-1-d-splice-alias"><a href="#1-2-1-d-splice-alias" class="headerlink" title="1.2.1 d_splice_alias()"></a><font size=3>1.2.1 d_splice_alias()</font></h4><p>而在这个函数的最后一部分，<a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n2897">d_splice_alias()</a> 函数中，将新创建的dentry与找到的inode进行关联，并顺便将新的dentry加入到哈希表中，这样下次执行 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1554">lookup_fast()</a> 函数时，即可迅速找到可用的dentry。下面看 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n2897">d_splice_alias()</a> 函数的定义：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> <span class="title class_">dentry</span> *<span class="built_in">d_splice_alias</span>(<span class="keyword">struct</span> inode *inode, <span class="keyword">struct</span> dentry *dentry)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">IS_ERR</span>(inode))</span><br><span class="line">		<span class="keyword">return</span> <span class="built_in">ERR_CAST</span>(inode);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">BUG_ON</span>(!<span class="built_in">d_unhashed</span>(dentry));</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (!inode)</span><br><span class="line">		<span class="keyword">goto</span> out;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">security_d_instantiate</span>(dentry, inode);<span class="comment">// 暂不分析该函数，可认为该函数什么都不做</span></span><br><span class="line">	<span class="built_in">spin_lock</span>(&amp;inode-&gt;i_lock);</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">S_ISDIR</span>(inode-&gt;i_mode)) &#123; <span class="comment">// 判断是否为文件夹，if 判断为 1</span></span><br><span class="line">		<span class="keyword">struct</span> <span class="title class_">dentry</span> *<span class="keyword">new</span> = __d_find_any_alias(inode);<span class="comment">//我们只考虑最简单的情况，一般情况下 new == NULL</span></span><br><span class="line">		<span class="keyword">if</span> (<span class="built_in">unlikely</span>(<span class="keyword">new</span>)) &#123;<span class="comment">//一般情况下， if 判断为 0</span></span><br><span class="line">			<span class="comment">//......</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">out:</span><br><span class="line">	__d_add(dentry, inode);</span><br><span class="line">	<span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">EXPORT_SYMBOL</span>(d_splice_alias);</span><br></pre></td></tr></table></figure>

<p>可见，这个函数里面只调用了<a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n2561">__d_add()</a> 函数。该函数定义如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">void</span> __d_add(<span class="keyword">struct</span> dentry *dentry, <span class="keyword">struct</span> inode *inode)</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">struct</span> <span class="title class_">inode</span> *dir = <span class="literal">NULL</span>;</span><br><span class="line">	<span class="type">unsigned</span> n;</span><br><span class="line">	<span class="built_in">spin_lock</span>(&amp;dentry-&gt;d_lock);</span><br><span class="line">	<span class="keyword">if</span> (<span class="built_in">unlikely</span>(<span class="built_in">d_in_lookup</span>(dentry))) &#123; <span class="comment">// 此处if 判断为 1，但这里我们并不做具体分析</span></span><br><span class="line">		dir = dentry-&gt;d_parent-&gt;d_inode;</span><br><span class="line">		n = <span class="built_in">start_dir_add</span>(dir);</span><br><span class="line">		__d_lookup_done(dentry);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span> (inode) &#123; <span class="comment">// if 判断为 1</span></span><br><span class="line">		<span class="type">unsigned</span> add_flags = <span class="built_in">d_flags_for_inode</span>(inode);</span><br><span class="line">		<span class="built_in">hlist_add_head</span>(&amp;dentry-&gt;d_u.d_alias, &amp;inode-&gt;i_dentry);</span><br><span class="line">		<span class="built_in">raw_write_seqcount_begin</span>(&amp;dentry-&gt;d_seq);</span><br><span class="line">		__d_set_inode_and_type(dentry, inode, add_flags); <span class="comment">// 设置dentry的inode和flags</span></span><br><span class="line">		<span class="built_in">raw_write_seqcount_end</span>(&amp;dentry-&gt;d_seq);</span><br><span class="line">		<span class="built_in">fsnotify_update_flags</span>(dentry);</span><br><span class="line">	&#125;</span><br><span class="line">	__d_rehash(dentry); <span class="comment">// 此时新创建的dentry 已经完全可用，将其加入到 哈希表 中</span></span><br><span class="line">	<span class="keyword">if</span> (dir) <span class="comment">// if 判断为 1，但此处不做分析</span></span><br><span class="line">		<span class="built_in">end_dir_add</span>(dir, n);</span><br><span class="line">	<span class="built_in">spin_unlock</span>(&amp;dentry-&gt;d_lock);</span><br><span class="line">	<span class="keyword">if</span> (inode)</span><br><span class="line">		<span class="built_in">spin_unlock</span>(&amp;inode-&gt;i_lock);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可见，在这个函数中，最终设置了dentry的inode，并将其加入到哈希表中，如此一来，下次可直接通过 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1554">lookup_fast()</a> 函数找到该dentry。</p>
<h4 id="1-2-2-总结"><a href="#1-2-2-总结" class="headerlink" title="1.2.2 总结"></a><font size=3>1.2.2 总结</font></h4><p><a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n2561">__d_add()</a> 函数返回后，回到 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/dcache.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n2897">d_splice_alias()</a> 函数，之后又返回到 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/ext4/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1560">ext4_lookup()</a> 函数，之后又返回到 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1642">__lookup_slow()</a> 函数， <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1642">__lookup_slow()</a> 函数紧接着调用 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/include/linux/dcache.h?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n364">lookup_done()</a> 函数，这个函数只是处理并发性问题，这里我们不考虑，可以认为该函数什么都没做。在这之后， <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1642">__lookup_slow()</a> 函数返回至 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1681">lookup_slow()</a> 函数，然后继续返回到 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1789">walk_component()</a> 函数，后面执行的操作和我们所《01嵌入式开发&#x2F;02IMX6ULL平台&#x2F;LV05-系统镜像&#x2F;LV05-03-Kernel-05-03-02-open函数解析1.md》里面分析的一样。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241225104517989.png" alt="image-20241225104517989"  />

<h1 id="三、-lookup-open"><a href="#三、-lookup-open" class="headerlink" title="三、 lookup_open()"></a><font size=3>三、 lookup_open()</font></h1><p>注意，如果是路径名的最后一部分不在dcache中，那么创建dentry以及查找inode的操作不在 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n1681">lookup_slow()</a> 函数中进行，而是在 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n3257">do_last()</a> 函数中，通过调用 <a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n3113">lookup_open()</a> 函数来完成，调用的部分在这一行<a target="_blank" rel="noopener" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/fs/namei.c?h=v4.19.71&id=e7d2672c66e4d3675570369bf20856296da312c4#n3324">do_last() &rarr; lookup_open()</a> 与上述过程略有不同，但基本过程是一样的，这里就不详细去分析了。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241225113903443.png" alt="image-20241225113903443"  />

<h1 id="四、总结"><a href="#四、总结" class="headerlink" title="四、总结"></a><font size=3>四、总结</font></h1><p>分析到现在，我们对open的分析涉及到这些函数了:</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/LV05-03-Kernel-05-03-03-open%E5%87%BD%E6%95%B0%E8%A7%A3%E6%9E%902/img/image-20241225114110536.png" alt="image-20241225114110536"  />



<blockquote>
<p>参考资料</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/assiduous_me/article/details/124899439">Linux中open命令实现原理以及源码分析_linux open-CSDN博客</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/arm7star/article/details/78668951">linux文件描述符分配实现详解(基于ARM处理器)_fdtable-CSDN博客</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/bgao86/article/details/79346389">Linux 系统调用之open(一）_linux 系统调用之open(一)-CSDN博客</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/bgao86/article/details/79394956">Linux 系统调用之open（二）_系统调用open 会走到lookup吗-CSDN博客</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/d22d4c9b.html">LV05-03-Kernel-05-03-03-open函数解析2</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2024年12月17日 - 23:25</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/d22d4c9b.html" title="LV05-03-Kernel-05-03-03-open函数解析2">https://sumumm.github.io/post/d22d4c9b.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV05-%E7%B3%BB%E7%BB%9F%E9%95%9C%E5%83%8F/" rel="tag"><i class="fa fa-tag"></i> LV05-系统镜像</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/97a3cc8e.html" rel="prev" title="LV06-03-chrdev-01-字符设备基础">
                  <i class="fa fa-angle-left"></i> LV06-03-chrdev-01-字符设备基础
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/9cc69d79.html" rel="next" title="LV05-03-Kernel-05-03-02-open函数解析1">
                  LV05-03-Kernel-05-03-02-open函数解析1 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
